from flask import g, jsonify
from flask import redirect
from flask import request

from info.modules.profile import profile_blu
from flask import render_template

from info.utils.common import user_login_data
from info.utils.response_code import RET


@profile_blu.route("/info")
@user_login_data
def user_info():
    user=g.user
    if not user: #代表没有登陆，重定向到首页
        return redirect("/")


    data={
        "user":user.to_dict(),
    }

    return render_template("news/user.html",data=data)

@profile_blu.route("/base_info",methods=["get","post"])
@user_login_data
def base_info():
    user=g.user
    if not user: #代表没有登陆，重定向到首页
        return redirect("/")

    if request.method=="GET":
        data={
            "user":user.to_dict(),
        }

    else:
        nick_name=request.json.get("nick_name")
        gender = request.json.get("gender")
        signature = request.json.get("signature")

        if not all([nick_name,gender,signature]):
            return jsonify(errno=RET.DATAERR,errmsg="参数错误")
        if gender not in ["MAN","WOWEN"]:
            return jsonify(errno=RET.DATAERR,errmsg="参数错误")


        user.nick_name=nick_name
        user.gender=gender
        user.signature=signature

        return jsonify(errno=RET.OK,errmsg="ok")




    return render_template("news/user_base_info.html",data=data)